// pages/operat/fleedTools/returnGoods/returnApply/returnApply.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   returnModel: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   let data = LApp.pages.get("fleedTools/returnGoods/scanCode/scanCode").data;
   this.setData({
    returnModel: data.returnInfo
   })
   if (this.data.returnModel.ProductID != 0) {
    this.productShow(this.data.returnModel.ProductID);
   }
   else
    this.getproductList();
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  GetFormCode: function (data) {
    var formCode = LApp._formatTime("yyyyMMddhhmmss");
    if (data) {
      formCode = data + "-" + formCode;
    }
    this.data.returnModel.FormCode = formCode;
    this.setData(this.data);
  },
  productShow: function (proId) {
   let _this = this;
   LApp._get({  
    url: 'nbapi/product/showproductinfo',
    data: { proId: proId },
    success: function (res) {
     _this.data.returnModel.ProductName = res.ProductName;
     _this.data.returnModel.ProductID = res.Id;
     _this.setData(_this.data);
     
     var dId = LApp.globalData.userInfo.Distributor;
     _this.GetFormCode(dId + "-" + res.Id);
    }
   })
  },
  bindModelChange: function (e) {
   wx.$$apply(e, this.data.returnModel);
  },
  pageComeBack: function () {
   wx.navigateBack({
    delta: 1
   })
  },
  saveReturn: function () {
   if (this.data.returnModel.ProductID == undefined || this.data.returnModel.ProductID == 0) {
    wx.showModal({
     title: '提示',
     content: '请选择产品',
     showCancel: false
    })
    return;
   }
   this.data.returnModel.FormType = 2;
   this.data.returnModel.CreateLoginID = LApp.globalData.userInfo.UserID;
   this.data.returnModel.CreateLoginName = LApp.globalData.userInfo.LoginName;
   this.data.returnModel.EnterpriseID = LApp.globalData.userInfo.EnterpriseId;
   LApp._post({
    url: 'nbapi/goods/return/add',
    data: this.data.returnModel,
    success: function (r) {
      wx.showModal({
        title: '保存成功！',
        icon: "success",
        success: function (res) {
          if (!res.cancel) {
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1];   //当前页面
            var prevPage = pages[pages.length - 2];  //上一个页面
            prevPage.setData({
              myDate: 1
            });
            wx.navigateBack(1);
          }
        }
      })
    }
   })
  },
  goSearchCode: function () {
   if (this.data.returnModel) {
    LApp.pages.add(this);
    wx.navigateTo({
     url: '../searchCode/searchCode',
    })
   }
  }
}) 